LIBRARY_PATH = /usr/local/lib
INCLUDE_PATH = /usr/local/include

#########Compilation Flags#######################################

#includes
CXXFLAGS+=-fPIC 
#-c 

CXXFLAGS+=-DMILI_NAMESPACE -pedantic -Wall -Wextra -ansi -I.

OBJECTS      = src/read_utils.o src/backbones_utils.o src/compressed_filer.o src/xtc_filer.o src/fragments_filer.o
FILES        = src/read_utils.cpp src/backbones_utils.cpp src/compressed_filer.cpp src/xtc_filer.cpp src/fragments_filer.cpp
INCLUDES     = prot-filer/backbones_utils.h prot-filer/angles.h prot-filer/base.h prot-filer/format_filer.h prot-filer/compressed_filer.h prot-filer/xtc_filer.h prot-filer/definitions.h prot-filer/read_utils.h prot-filer/cached_reader.h prot-filer/reader_factory.h prot-filer/fragments_filer.h


CXX          = g++

LDFLAGS = -lxdrfile


ifeq ($(DEBUG),on)
    CXXFLAGS+=-ggdb3 -Weffc++ 
endif

LIBS = -lxdrfile

prot-filer: $(OBJECTS) 
		$(CXX) $(CXXFLAGS) -shared -Wl,-soname,libprot-filer.so.1 -o libprot-filer.so.1.0 $(OBJECTS) $(LDFLAGS) $(LIBS)
		
tools: 
		$(CXX) $(CXXFLAGS) $(LIBS) -lprot-filer tools/read_xtc.cpp -o read_xtc
		$(CXX) $(CXXFLAGS) $(LIBS) -lprot-filer tools/read_compressed.cpp -o read_compressed

.PHONY: cleanall cleanobj install tools

cleanall : cleanobj 

clean : cleanall

cleanobj :
	rm -f *.o */*.o */*/*.o */*/*/*.o
	rm -f libprot-filer.so.1.0 libprot-filer.so.1.0 libprot-filer.a

install:
	cp libprot-filer.so.1.0 $(LIBRARY_PATH)
	ln -sf $(LIBRARY_PATH)/libprot-filer.so.1.0 $(LIBRARY_PATH)/libprot-filer.so.1
	ln -sf $(LIBRARY_PATH)/libprot-filer.so.1.0 $(LIBRARY_PATH)/libprot-filer.so

	mkdir -p $(INCLUDE_PATH)/prot-filer/clusterer
	cp $(INCLUDES) $(INCLUDE_PATH)/prot-filer
	ldconfig

uninstall:
	rm $(LIBRARY_PATH)/libprot-filer*
	rm -r $(INCLUDE_PATH)/prot-filer/
	ldconfig

# Find all sources with available tests
TEST_SRCS := $(patsubst ./%, %, $(shell find|egrep "_test\.cpp$$") )
TEST_BINS := $(patsubst %.cpp, %, $(TEST_SRCS))

test:
	@# Run all tests
	for TEST in $(TEST_BINS); do \
		make "$$TEST"_run; \
		done

%_test: %_test.cpp
	@# Compile the source and link with the real .o
	g++ $(OBJECTS) $@.cpp -o $@ \
		$(CXXFLAGS) $(LDFLAGS) \
		-lgtest_main -lgmock -lpthread

%_test_run: %_test
	@# Zomg magic magic - this will make the test and then run it
	./$<

